<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of evaluation_constraints_controller
 *
 * @author wilavel
 */
class EvaluationConstraintsController extends AppController {

    //put your code here
    var $name = "EvaluationConstraints";

    function index($evaluation_id) {
        $this->layout = "ajax";
        $this->set('evaluation_id', $evaluation_id);
        $this->EvaluationConstraint->recursive = 1;
        $this->set('condiciones', $this->EvaluationConstraint->find('all', array('conditions' => array('EvaluationConstraint.final_evaluation_id' => $evaluation_id, 'Constraint.tipo' => 'Proyecto'), 'fields' => array('EvaluationConstraint.id', 'EvaluationConstraint.descripcion', 'Constraint.nombre'))));
    }

    function add($evaluation_id) {
        $this->set('evaluation_id', $evaluation_id);
        $this->set('constraints', $this->EvaluationConstraint->Constraint->find('list', array('fields' => array('Constraint.id', 'Constraint.nombre'), 'conditions' => array('Constraint.tipo' => 'Proyecto'))));
        if (!empty($this->data)) {
            $this->data['EvaluationConstraint']['user_id'] = $this->Auth->user('id');
            if ($this->EvaluationConstraint->saveAll($this->data)) {
                $this->Session->setFlash('Registro adicionado con éxito', 'flash_custom');
                $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'index', $evaluation_id));
            }
        }
    }

    function edit($constraint_id) {
        $this->set('constraints', $this->EvaluationConstraint->Constraint->find('list', array('fields' => array('Constraint.id', 'Constraint.nombre'), 'conditions' => array('Constraint.tipo' => 'Proyecto'))));
        if (empty($this->data)) {
            $this->data = $this->EvaluationConstraint->find('first', array('conditions' => array('EvaluationConstraint.id' => $constraint_id), 'fields' => array('EvaluationConstraint.id', 'EvaluationConstraint.descripcion', 'Constraint.nombre', 'EvaluationConstraint.final_evaluation_id', 'EvaluationConstraint.constraint_id')));
        } else {
            if ($this->EvaluationConstraint->saveAll($this->data)) {
                $this->Session->setFlash('Registro editado con éxito', 'flash_custom');
                $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'index', $this->data['EvaluationConstraint']['final_evaluation_id']));
            }
        }
    }

    function edit_uprising($constraint_id) {
        $this->set('constraints', $this->EvaluationConstraint->Constraint->find('list', array('fields' => array('Constraint.id', 'Constraint.nombre'), 'conditions' => array('Constraint.tipo' => 'Proyecto'))));
        if (empty($this->data)) {
            $this->data = $this->EvaluationConstraint->find('first', array('conditions' => array('EvaluationConstraint.id' => $constraint_id), 'fields' => array('EvaluationConstraint.observaciones_levantamiento', 'EvaluationConstraint.fecha_levantamiento', 'EvaluationConstraint.calificacion', 'EvaluationConstraint.id', 'EvaluationConstraint.descripcion', 'Constraint.nombre', 'EvaluationConstraint.final_evaluation_id', 'EvaluationConstraint.constraint_id')));
        } else {
            //SE ENVIAN DATOS
            if ($this->data['EvaluationConstraint']['calificacion'] == "LEVANTADO") {
                //CALIFICACION ES IGUAL A LEVANTADO
                if (stristr($this->data['EvaluationConstraint']['archivo']['name'], ".pdf")) {
                    //EL ARCHIVO EXISTE.
                    if ($this->EvaluationConstraint->saveAll($this->data)) {

                        $codigo = $this->Session->read('cod');
                        $rutaArchivo = APP . "webroot" . "/" . "files" . "/" . $codigo;
                        if (!is_dir($rutaArchivo)) {
                            if (!mkdir($rutaArchivo)) {
                                echo "error creando archivo";
                                //redirect
                            }
                        }
                        $nombreArchivo = "LEVANTAMIENTO-$codigo-$constraint_id.pdf";
                        $rutaArchivo.= "/" . $nombreArchivo;


                        if (move_uploaded_file($this->data['EvaluationConstraint']['archivo']['tmp_name'], $rutaArchivo)) {
                            $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                            $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'total_index', $this->data['EvaluationConstraint']['final_evaluation_id']));
                        }
                    } else {
                        //NO PUDO GUARDAR LOS DATOS
                        $this->Session->setFlash('Error Guardando datos', 'flash_custom');
                    }
                } else {
                    $this->Session->setFlash('ERROR GUARDANDO LOS DATOS. Si la calificación es LEVANTADO, es obligatorio adjuntar archivo tipo pdf', 'flash_custom');
                    $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'total_index', $this->data['EvaluationConstraint']['final_evaluation_id']));
                }
            } else {
                //LA CALIFICACION ES DISTINTA A LEVANTADO
                if ($this->EvaluationConstraint->saveAll($this->data)) {
                    //SI EL ARCHIVO EXISTE SE GUARDA.
                    if (stristr($this->data['EvaluationConstraint']['archivo']['name'], ".pdf")) {

                        $codigo = $this->Session->read('cod');
                        $rutaArchivo = APP . "webroot" . "/" . "files" . "/" . $codigo;
                        if (!is_dir($rutaArchivo)) {
                            if (!mkdir($rutaArchivo)) {
                                echo "error creando archivo";
                                //redirect
                            }
                        }
                        $nombreArchivo = "LEVANTAMIENTO-$codigo-$constraint_id.pdf";
                        $rutaArchivo.= "/" . $nombreArchivo;


                        if (move_uploaded_file($this->data['EvaluationConstraint']['archivo']['tmp_name'], $rutaArchivo)) {
                            $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                            $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'total_index', $this->data['EvaluationConstraint']['final_evaluation_id']));
                        }
                    }

                    $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                    $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'total_index', $this->data['EvaluationConstraint']['final_evaluation_id']));
                } else {
                    $this->Session->setFlash('Error Guardando datos', 'flash_custom');
                }
            }
        }
    }

    function delete($id, $evaluation_id) {
        if ($this->EvaluationConstraint->delete($id)) {
            $this->Session->setFlash('Registro borrado con éxito', 'flash_custom');
            $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'index', $evaluation_id));
        }
    }

    function juridic_index($evaluation_id) {
        $this->layout = "ajax";
        $this->set('evaluation_id', $evaluation_id);
        $this->EvaluationConstraint->recursive = 1;
        $this->set('condiciones', $this->EvaluationConstraint->find('all', array('conditions' => array('EvaluationConstraint.final_evaluation_id' => $evaluation_id, 'Constraint.tipo' => 'Juridico'), 'fields' => array('EvaluationConstraint.id', 'EvaluationConstraint.descripcion', 'Constraint.nombre'))));
    }
    
    function juridic_add($evaluation_id) {
        $this->set('evaluation_id', $evaluation_id);
        $this->set('constraints', $this->EvaluationConstraint->Constraint->find('list', array('fields' => array('Constraint.id', 'Constraint.nombre'), 'conditions' => array('Constraint.tipo' => 'Juridico'))));
        if (!empty($this->data)) {
            $this->data['EvaluationConstraint']['user_id'] = $this->Auth->user('id');
            if ($this->EvaluationConstraint->saveAll($this->data)) {
                $this->Session->setFlash('Registro adicionado con éxito', 'flash_custom');
                $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'juridic_index', $evaluation_id));
            }
        }
    }

    function juridic_edit($constraint_id) {
        $this->set('constraints', $this->EvaluationConstraint->Constraint->find('list', array('fields' => array('Constraint.id', 'Constraint.nombre'), 'conditions' => array('Constraint.tipo' => 'Juridico'))));
        if (empty($this->data)) {
            $this->data = $this->EvaluationConstraint->find('first', array('conditions' => array('EvaluationConstraint.id' => $constraint_id), 'fields' => array('EvaluationConstraint.id', 'EvaluationConstraint.descripcion', 'Constraint.nombre', 'EvaluationConstraint.final_evaluation_id')));
        } else {
            if ($this->EvaluationConstraint->saveAll($this->data)) {
                $this->Session->setFlash('Registro editado con éxito', 'flash_custom');
                $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'juridic_index', $this->data['EvaluationConstraint']['final_evaluation_id']));
            }
        }
    }

    function juridic_delete($id, $evaluation_id) {
        if ($this->EvaluationConstraint->delete($id)) {
            $this->Session->setFlash('Registro borrado con éxito', 'flash_custom');
            $this->redirect(array('controller' => 'EvaluationConstraints', 'action' => 'juridic_index', $evaluation_id));
        }
    }

    function total_index() {
        $this->layout = "ajax";
        App::Import('model', 'FinalEvaluation');
        $evaluacionFinal = new FinalEvaluation();

        $proyect_id = $this->Session->read('proy_id');

        App::Import('model', 'Proyect');
        $proyect = new Proyect();
        $publicado = $proyect->field('Proyect.publicado', array('Proyect.id' => $proyect_id));

        $this->set("codigo", $this->Session->read('cod'));

        if ($proyect_id != "") {
            if ($publicado == 1) {
                $evaluation_id = $evaluacionFinal->field('FinalEvaluation.id', array('FinalEvaluation.proyect_id' => $proyect_id), 'FinalEvaluation.id DESC');
                $this->set('evaluation_id', $evaluation_id);
                $this->EvaluationConstraint->recursive = 1;
                $this->set('condiciones', $this->EvaluationConstraint->find('all', array('conditions' => array('EvaluationConstraint.final_evaluation_id' => $evaluation_id), 'fields' => array('EvaluationConstraint.id', 'EvaluationConstraint.calificacion', 'Constraint.nombre', 'EvaluationConstraint.observaciones_levantamiento', 'EvaluationConstraint.fecha_levantamiento', 'EvaluationConstraint.descripcion'))));
            } else {
                $this->Session->setFlash('Este proyecto aún no ha sido publicado.', 'flash_custom');
                $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
            }
        } else {
            $this->Session->setFlash('No ha seleccionado Proyecto', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    function print_letter($evaluation_id) {
        $this->layout = "pdf";
        $this->EvaluationConstraint->recursive = 1;
        $proyect_id = $this->Session->read('proy_id');
        $options = array();
        $options['joins'] = array(
            array(
                'table' => 'proposers',
                'type' => 'left',
                'alias' => 'Proposer',
                'conditions' => array('Proyect.id=Proposer.proyect_id'),
            ),
            array(
                'table' => 'calls',
                'type' => 'left',
                'alias' => 'Call',
                'conditions' => array('Call.id=Proyect.call_id'),
            ),
            array(
                'table' => 'branches',
                'type' => 'left',
                'alias' => 'Branch',
                'conditions' => array('Branch.id=Proyect.branch_id'),
            ),
            array(
                'table' => 'cities',
                'type' => 'left',
                'alias' => 'City',
                'conditions' => array('City.id=Proposer.city_id'),
            ),
            array(
                'table' => 'departaments',
                'type' => 'left',
                'alias' => 'Departamento',
                'conditions' => array('Proyect.departament_id=Departamento.id'),
            ),
            array(
                'table' => 'departaments',
                'type' => 'left',
                'alias' => 'Departament',
                'conditions' => array('City.departament_id=Departament.id'),
            ),
        );
        $options['conditions'] = array('Proyect.id' => $proyect_id);
        $options['fields'] = array('Departamento.name', 'Proposer.nombre', 'Proposer.numero_identificacion', 'Proposer.proposer_type', 'Proposer.rep_nombres', 'Proposer.rep_apellidos', 'Proposer.rep_cedula', 'Proposer.direccion', 'Proposer.telefono_fijo', 'Proposer.telefono_celular', 'Departament.name', 'City.name', 'Branch.director', 'Proyect.codigo', 'Proyect.id', 'Call.nombre', 'Branch.nombre');

        App::Import('model', 'Proyect');
        $Proyect = new Proyect();

        $Proyect->recursive = -1;
        $proponente = $Proyect->find('first', $options);
        $this->set('proponente', $proponente);
        $this->set('condiciones', $this->EvaluationConstraint->find('all', array('conditions' => array('EvaluationConstraint.final_evaluation_id' => $evaluation_id), 'fields' => array('EvaluationConstraint.id', 'EvaluationConstraint.calificacion', 'Constraint.nombre', 'Constraint.id', 'Constraint.tipo', 'EvaluationConstraint.observaciones_levantamiento', 'EvaluationConstraint.fecha_levantamiento', 'EvaluationConstraint.descripcion'))));
    }

}

?>
